<template>
  <el-dialog title="新增员工" :visible="showDialog" @close="btnCancel">
    <!-- 表单 -->
    <el-form ref="employeeForm" label-width="120px" :model="formData" :rules="rules">
      <el-form-item label="姓名" prop="username">
        <el-input v-model="formData.username" placeholder="请输入姓名" style="width:100%" />
      </el-form-item>
      <el-form-item label="手机" prop="mobile">
        <el-input v-model="formData.mobile" placeholder="请输入手机号" style="width:100%" />
      </el-form-item>
      <el-form-item label="入职时间" prop="timeOfEntry">
        <el-date-picker v-model="formData.timeOfEntry" placeholder="请选择入职时间" style="width:100%" />
      </el-form-item>
      <el-form-item label="聘用形式" prop="formOfEmployment">
        <el-select v-model="formData.formOfEmployment" placeholder="请选择" style="width:100%">
          <el-option v-for="item in EmployeeEnum.hireType" :key="item.id" :value="item.id" :label="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="工号" prop="workNumber">
        <el-input v-model="formData.workNumber" placeholder="请输入工号" style="width:100%" />
      </el-form-item>
      <el-form-item label="部门" prop="departmentName">
        <el-input v-model="formData.departmentName" placeholder="请选择部门" @focus="getDepartments" />
        <!-- 树形组件 -->
        <el-tree
          v-if="showTree"
          v-loading="loading"
          class="departmentTree"
          :data="treeData"
          :props="{label:'name'}"
          :default-expand-all="true"
          @node-click="departmentSelect"
        />

      </el-form-item>
      <el-form-item label="转正时间">
        <el-date-picker v-model="formData.correctionTime" placeholder="请选择转正时间" style="width:100%" />
      </el-form-item>
    </el-form>
    <!-- footer插槽 -->
    <template v-slot:footer>
      <el-row type="flex" justify="center">
        <el-col :span="6">
          <el-button size="small" @click="btnCancel">取消</el-button>
          <el-button type="primary" size="small" @click="btnOK">确定</el-button>
        </el-col>
      </el-row>
    </template>
  </el-dialog>
</template>

<script>
import { getDepartments } from '@/api/departments'
import { addEmployee } from '@/api/employees'
import { tranListToTreeData } from '@/utils'
import EmployeeEnum from '@/api/constant/employees'
export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      formData: {
        username: '',
        mobile: '',
        formOfEmployment: '',
        workNumber: '',
        departmentName: '',
        timeOfEntry: '',
        correctionTime: ''
      },
      rules: {
        username: [{ required: true, message: '用户姓名不能为空', trigger: 'blur' }, {
          min: 1, max: 4, message: '用户姓名为1-4位'
        }],
        mobile: [{ required: true, message: '手机号不能为空', trigger: 'blur' }, {
          pattern: /^1[3-9]\d{9}$/, message: '手机号格式不正确', trigger: 'blur'
        }],
        formOfEmployment: [{ required: true, message: '聘用形式不能为空', trigger: 'blur' }],
        workNumber: [{ required: true, message: '工号不能为空', trigger: 'blur' }],
        departmentName: [{ required: true, message: '部门不能为空', trigger: 'change' }],
        timeOfEntry: [{ required: true, message: '入职时间', trigger: 'blur' }]
      },
      showTree: false,
      treeData: [],
      loading: false,
      EmployeeEnum
    }
  },
  methods: {
    async getDepartments() {
      // 弹出树形组件
      this.showTree = true
      // 加载状态设为 true
      this.loading = true
      // 获取全部部门数据
      const res = await getDepartments()
      console.log(res)
      // 转换为树形
      this.treeData = tranListToTreeData(res.depts, '')
      // 加载状态设为 false
      this.loading = false
    },
    departmentSelect(node) {
      // 这个函数是部门被点击时触发
      // 可以接收到被点击的部门数据
      console.log(node)
      this.formData.departmentName = node.name
      this.showTree = false
    },
    // 确认按钮
    async  btnOK() {
      try {
        // 1. 校验表单
        await this.$refs.employeeForm.validate()
        // 2. 发请求
        await addEmployee(this.formData)
        // 3. 更新页面
        this.$parent.getEmployeeList()
        // 4. 提醒用户
        this.$message.success('操作成功')
        // 5. 关闭弹窗
        this.$parent.showDialog = false
      } catch (error) {
        console.log(error)
      }
    },
    // 取消操作
    btnCancel() {
      // 将整个表单恢复原样
      this.formData = {
        username: '',
        mobile: '',
        formOfEmployment: '',
        workNumber: '',
        departmentName: '',
        timeOfEntry: '',
        correctionTime: ''
      }
      // 清理表单绑定过的数据, 和报错提示
      this.$refs.employeeForm.resetFields()
      // 通知父组件关闭弹窗
      this.$emit('update:showDialog', false)
    }
  }
}
</script>

<style lang="scss" scoped>

.departmentTree {
    position: absolute;
    width: 100%;
    z-index: 9;
    border: 1px solid #888;
    height: 300px;
    overflow-y: scroll;
}
</style>
